<?php
/**
 * Desc: 文件上传接口
 * Auth: Shaoming Lu<lushao1012@163.com>
 * Date: 2018/6/7
 * Time: 14:18
 */
namespace App\Http\Controllers;

use App\Http\Services\AjaxService;
use App\Http\Services\MCore;
use Illuminate\Http\Request;
use App\Http\Services\DB;
class UploadController extends BaseController
{
    /**
     * @access  public
     * @author  Lu Shaoming<lushao1012@163.com>
     * @method  GET
     * @param Request $request
     */
    public function index(Request $request)
    {
        $type = MCore::get_not_empty_var('type', $request, '未知的文件类型要求');
        $file = $_FILES['file'];

        $accept_type = MCore::get_accept_file_type($type);
        $ext = substr($file['name'], strripos($file['name'], '.'));
        if (!in_array($ext, $accept_type)) {
//            var_dump($ext);exit;
            AjaxService::ajax_return(['error' => '文件类型错误']);
        } else {
            $file_name = 'tempUpload/'.time().mt_rand(1000, 9999).$ext;
            $flag = move_uploaded_file($file['tmp_name'], $file_name);
            $file_name = '/'.$file_name;
            if ($flag === true) {
                AjaxService::ajax_return(['msg' => 'SUCCESS'], ['file_path' => $file_name]);
            } else {
                AjaxService::ajax_return(['error' => '上传文件失败，请重试！']);
            }
        }

    }
}